<?php
namespace Shopiuu\Model;

use Shopiuu\Http\RequestCore;

class Product extends Model{
    
    public function __construct($accessToken, $endpoint)
    {
        parent::__construct($accessToken, $endpoint);
    }

    /**
     * Create product
     * @param array $params array
        (
            [category] => Fashion Secret Dress,kawai Dress
            [name] => Halloween Clothes 9996
            [tags] => New Product,Hot Product
            [brand] => 
            [supplier] => 
            [sort] => 
            [images] => Array
                (
                    [0] => http://image.example.com/1/goods/2020-11-01/7677ebe1faa16638.jpg
                    [1] => http://image.example.com/1/goods/2020-11-01/93e9bef6b2b1c927.jpg
                )

            [price] => 100
            [mprice] => 
            [cprice] => 
            [itemno] => LC9996137-1
            [stock] => 100
            [instock] => 1
            [minnum] => 
            [option_name] => Array
                (
                    [0] => Color
                    [1] => Size
                )

            [option_value] => Array
                (
                    [0] => Red,Yellow
                    [1] => S,M,L
                )

            [variants] => Array
                (
                    [0] => Array
                        (
                            [spec] => Red,S
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-1
                            [barcode] => 
                        )

                    [1] => Array
                        (
                            [spec] => Red,M
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-2
                            [barcode] => 
                        )

                    [2] => Array
                        (
                            [spec] => Red,L
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-3
                            [barcode] => 
                        )

                    [3] => Array
                        (
                            [spec] => Yellow,S
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-4
                            [barcode] => 
                        )

                    [4] => Array
                        (
                            [spec] => Yellow,M
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-5
                            [barcode] => 
                        )

                    [5] => Array
                        (
                            [spec] => Yellow,L
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-6
                            [barcode] => 
                        )

                )

            [weight] => 0.5
            [title] => 
            [keywords] => 
            [descript] => 
            [diy_url] => 
            [details] => <p>Halloween Clothes 001</p>
        )

     * @return ResponseCore
     */
    public function add($params)
    {
        $request = $this->getRequest('/openApi/products/add');
        $result = $request->set_body(json_encode($params))->set_method('POST')->send_request(true);
        return $result;
    }

    /**
     * Update product
     * @param array $paramss array
        (
            [id] => 1
            [category] => Fashion Secret Dress,kawai Dress
            [name] => Halloween Clothes 9996
            [tags] => New Product,Hot Product
            [brand] => 
            [supplier] => 
            [sort] => 
            [images] => Array
                (
                    [0] => http://image.example.com/1/goods/2020-11-01/7677ebe1faa16638.jpg
                    [1] => http://image.example.com/1/goods/2020-11-01/93e9bef6b2b1c927.jpg
                )

            [price] => 100
            [mprice] => 
            [cprice] => 
            [itemno] => LC9996137-1
            [stock] => 100
            [instock] => 1
            [minnum] => 
            [option_name] => Array
                (
                    [0] => Color
                    [1] => Size
                )

            [option_value] => Array
                (
                    [0] => Red,Yellow
                    [1] => S,M,L
                )

            [variants] => Array
                (
                    [0] => Array
                        (
                            [spec] => Red,S
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-1
                            [barcode] => 
                        )

                    [1] => Array
                        (
                            [spec] => Red,M
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-2
                            [barcode] => 
                        )

                    [2] => Array
                        (
                            [spec] => Red,L
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-3
                            [barcode] => 
                        )

                    [3] => Array
                        (
                            [spec] => Yellow,S
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-4
                            [barcode] => 
                        )

                    [4] => Array
                        (
                            [spec] => Yellow,M
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-5
                            [barcode] => 
                        )

                    [5] => Array
                        (
                            [spec] => Yellow,L
                            [price] => 99
                            [weight] => 0.5
                            [stock] => 99
                            [sku_code] => LC403138-6
                            [barcode] => 
                        )

                )

            [weight] => 0.5
            [title] => 
            [keywords] => 
            [descript] => 
            [diy_url] => 
            [details] => <p>Halloween Clothes 001</p>
        )
     * The id is required, others is optional.
     * @return ResponseCore
     */
    public function update($params)
    {
        $request = $this->getRequest('/openApi/products/update');
        $result = $request->set_body(json_encode($params))->set_method('POST')->send_request(true);
        return $result;
    }

    /**
     * Get products
     * @param array $params array(
        "pagenum"=> 1 //页码
        "pagesize"=> 20 //每页数量
        "search"=> "keywords" //搜索标题
        "itemno"=> "ITEM001" //搜索商品编号
        "orderfeild"=> "id" //排序字段
        "ordersort"=> "desc" //排序方式
        "start_date"=> "2025-01-01" //开始日期
        "end_date"=> "2025-01-01" //结束日志
     )
     * @return ResponseCore
     */
    public function getList($params)
    {
        $request = $this->getRequest('/openApi/products/lists');
        $result = $request->set_body(json_encode($params))->set_method('POST')->send_request(true);
        return $result;
    }
    
    /**·1
     * Delete article
     * @param array $params array("id"=>"25")
     * @return ResponseCore
     */
    public function delete($params)
    {
        $request = $this->getRequest('/openApi/products/delete');
        $result = $request->set_body(json_encode($params))->set_method('POST')->send_request(true);
        return $result;
    }

}